using System.Text.Json;

public class MatchClient(string baseUrl)
{
    private readonly HttpClient _httpClient = new();
    private readonly string _baseUrl = baseUrl;

    // 修改返回类型为允许 Dictionary<string, object> 为可空
    public async Task<(bool success, Dictionary<string, object>? result, string message)> MatchImagesBase64(string mainBase64, string templateBase64, double threshold = 0.8)
    {
        var url = $"{_baseUrl}/match/base64";
        var formData = new Dictionary<string, string>
        {
            { "main_base64", mainBase64 },
            { "template_base64", templateBase64 },
            { "threshold", threshold.ToString() }
        };
        var content = new FormUrlEncodedContent(formData);

        try
        {
            var response = await _httpClient.PostAsync(url, content);
            response.EnsureSuccessStatusCode();
            string responseContent = await response.Content.ReadAsStringAsync();
            Dictionary<string, object>? result = JsonSerializer.Deserialize<Dictionary<string, object>>(responseContent);
            return (true, result, "");
        }
        catch (HttpRequestException e)
        {
            return (false, null, e.Message);
        }
    }
}